import { provide } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import api from "@/utils";
import { TableUrl } from "@/models/TableSetting";

export default function ReceiptTableApprove(
  props: {
    url: TableUrl;
    approveName: string;
  },
  getRecord: () => void
) {
  // 销售员提交事件
  const submitRecord = (row: Record<string, unknown>) => {
    ElMessageBox.confirm("确认提交本条记录吗", "提交", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "success",
    })
      .then(() => {
        api.http
          .postJson(props.url.submitApproveUrl + "提交", row)
          .then(() => {
            getRecord();
            ElMessage.success("提交成功!");
          })
          .catch((err) => {
            console.log(err);
          });
      })
      .catch((cancel) => {
        console.log(cancel);
      });
  };
  provide("submitRecord", submitRecord);

  // 审批通过事件
  const agreeRecord = (row: Record<string, unknown>) => {
    ElMessageBox.prompt("备注说明", "审批通过", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "success",
    })
      .then(({ value }) => {
        api.http
          .postJson(
            props.url.approveUrl +
              "审批通过/" +
              value +
              "/" +
              sessionStorage.getItem("token") +
              "/" +
              props.approveName,
            row
          )
          .then(() => {
            getRecord();
            ElMessage.success("审批成功!");
          })
          .catch((err) => {
            console.log(err);
          });
      })
      .catch((cancel) => {
        console.log(cancel);
      });
  };
  provide("agreeRecord", agreeRecord);

  // 审批拒绝事件
  const rejectRecord = (row: Record<string, unknown>) => {
    ElMessageBox.prompt("备注说明", "审批拒绝", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "error",
    })
      .then(({ value }) => {
        api.http
          .postJson(
            props.url.approveUrl +
              "审批拒绝/" +
              value +
              "/" +
              sessionStorage.getItem("token") +
              "/" +
              props.approveName,
            row
          )
          .then(() => {
            getRecord();
            ElMessage.success("审批成功!");
          })
          .catch((err) => {
            console.log(err);
          });
      })
      .catch((cancel) => {
        console.log(cancel);
      });
  };
  provide("rejectRecord", rejectRecord);
  return {
    submitRecord,
    agreeRecord,
    rejectRecord,
  };
}
